package com.bnz.controller;

import com.bnz.entity.Classes;
import com.bnz.entity.PageResult;
import com.bnz.entity.Student;
import com.bnz.service.ClassesService;
import com.bnz.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("student")
public class StudentController {
    // 引入StudentService
    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassesService classesService;

    // 1、查询所有的学生
    @GetMapping("list")
    public String findAll(Model model) { // 导入model容器
        // 1.1、查询所有学生
        List<Student> students = studentService.findAll();
        // 1.2、将查到的数据放进model
        model.addAttribute("students", students);
        // 1.3、返回逻辑视图
        return "student/list";
    }

    // 2、分页查询
    @GetMapping("findByPage")
    public String findByPage(@RequestParam(defaultValue = "1", required = false) int page,
                             @RequestParam(defaultValue = "5", required = false) int pagesize,
                             Model model) {
        // 2.1、分页查询数据
        PageResult<Student> pr = studentService.findByPage(page, pagesize);
        //2.2 放到model中
        model.addAttribute("pr", pr);
        //2.3 查询所有的班级
        List<Classes> classes = classesService.findAll();
        //2.4 将班级列表放天model
        model.addAttribute("classes", classes);
        //2.3 返回
        return "student/list";
    }
}


